﻿using System;
using System.Collections.Generic;
using System.Globalization;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using DataService;

public partial class Devices_AddDevice : BasePage
{
    private IList<Device.PlanType> Plans;
    private IList<Device.OperatingSystem> OSs;
    private DeviceService service = new DeviceService();
    private IList<Device.Memory> Memorys;
    private IList<Device.CPUs> CPUs;
    private IList<Device.DiskSpace> DiskPlaces;
    private Device.OperatingSystem OS;
    private int CPUId = 0;
    private int RamId = 0;
    private int DiskPlaceId = 0;
    private bool IsCustom = false;
    private string Price = "0";

    public Device.PlanType PlanSelected;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (UserLogin != null)
        {
            Plans = service.GetListPlansTemplate();
            OSs = service.GetListOS();
            Memorys = service.GetListMemory();
            CPUs = service.GetListCPU();
            DiskPlaces = service.GetListDiskPlace();
            if (!IsPostBack)
            {
                Initial();
                DataBind();
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }
    }

    public void Initial()
    {
        if (Plans != null)
        {
            dlPlan.DataSource = Plans;
            radListPlans.SelectedIndex = 0;
            SetPlanSelected(0);
            NextBilling(Convert.ToSingle(radListPlans.SelectedValue));
        }
        if (OSs != null)
        {
            ddlOS.DataSource = OSs;
            ddlOS.DataMember = "Id";
            ddlOS.DataValueField = "Name";
            OS = OSs[0];
        }
        if (Memorys != null)
        {
            ddlMemory.DataSource = Memorys;
            ddlMemory.DataMember = "Id";
            ddlMemory.DataValueField = "Details";
        }
        if (CPUs != null)
        {
            ddlCpu.DataSource = CPUs;
            ddlCpu.DataMember = "Id";
            ddlCpu.DataValueField = "Details";
        }
        if (DiskPlaces != null)
        {
            ddlDiskPlace.DataSource = DiskPlaces;
            ddlDiskPlace.DataMember = "Id";
            ddlDiskPlace.DataValueField = "Details";
        }
        DataBind();
    }

    private void ShowHideCustomOption(bool value)
    {
        divCustomOption.Visible = value;
    }

    private void NextBilling(float price)
    {
        lblPriceTotal.Text = "$" + price.ToString();
        txtPrice.Text = "$" + price.ToString() + "/ Month";
        float PriceOfWeek = price / 7;
        lblPriceOfWeek.Text = "$" + PriceOfWeek.ToString("#.#0");
        lblNextDate.Text = "Monday, ";
        lblNextDate.Text += GetFirstDateOfWeek(DateTime.Now.AddDays(7)).ToShortDateString();
        Price = price.ToString();
    }

    public void BillingCustomPlan(string PriceCPU, string PriceMemory, string PriceDiskPlace)
    {
        try
        {
            float cpu = Convert.ToSingle(PriceCPU);
            float memory = Convert.ToSingle(PriceMemory);
            float disk = Convert.ToSingle(PriceDiskPlace);
            NextBilling(cpu + memory + disk);
        }
        catch
        {

        }
    }

    public DateTime GetFirstDateOfWeek(DateTime dayInWeek)
    {
        DayOfWeek firstDay = DayOfWeek.Monday;
        DateTime firstDayInWeek = dayInWeek.Date;
        while (firstDayInWeek.DayOfWeek != firstDay)
            firstDayInWeek = firstDayInWeek.AddDays(-1);
        return firstDayInWeek;
    }

    private void SetPlanSelected(int index)
    {
        switch (index)
        {
            case 0:
                ShowHideCustomOption(false);
                NextBilling(Convert.ToSingle(radListPlans.SelectedValue));
                PlanSelected = Plans[index];
                IsCustom = false;
                break;
            case 1:
                ShowHideCustomOption(false);
                NextBilling(Convert.ToSingle(radListPlans.SelectedValue));
                PlanSelected = Plans[index];
                IsCustom = false;
                break;
            case 2:
                ShowHideCustomOption(false);
                NextBilling(Convert.ToSingle(radListPlans.SelectedValue));
                PlanSelected = Plans[index];
                IsCustom = false;
                break;
            case 3:
                ShowHideCustomOption(true);
                SelectedPlan(Convert.ToSingle(radListPlans.SelectedValue));
                IsCustom = true;
                break;
            default:
                break;
        }
    }

    protected void radListPlans_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = radListPlans.SelectedIndex;
        SetPlanSelected(index);
    }

    private void SelectedPlan(float price)
    {
        if (CPUs != null && Memorys != null && DiskPlaces != null && ddlCpu.SelectedIndex >= 0 && ddlMemory.SelectedIndex >= 0 && ddlDiskPlace.SelectedIndex >= 0)
        {
            PlanSelected = new Device.PlanType();
            PlanSelected.CPUs = Convert.ToString(CPUs[ddlCpu.SelectedIndex].Id);
            CPUId = CPUs[ddlCpu.SelectedIndex].Id;
            PlanSelected.RAM = Convert.ToString(Memorys[ddlMemory.SelectedIndex].Id);
            RamId = Memorys[ddlMemory.SelectedIndex].Id;
            PlanSelected.DiskQuota = Convert.ToString(DiskPlaces[ddlDiskPlace.SelectedIndex].Id);
            DiskPlaceId = DiskPlaces[ddlDiskPlace.SelectedIndex].Id;
            Price = price.ToString();
            NextBilling(price);
            BillingCustomPlan(CPUs[ddlCpu.SelectedIndex].Price, Memorys[ddlMemory.SelectedIndex].Price, DiskPlaces[ddlDiskPlace.SelectedIndex].Price);
        }
    }
    protected void btnRequest_Click(object sender, EventArgs e)
    {
        if (Plans != null && UserLogin != null)
        {
            if (radListPlans.SelectedIndex >= 0)
            {
                SetPlanSelected(radListPlans.SelectedIndex);
                OS = OSs[ddlOS.SelectedIndex];
                lblMessage.Visible = true;
                service.AddRequestCustomer(UserLogin.Id, PlanSelected.Id, CPUId, RamId, DiskPlaceId, OS.Id, IsCustom);
                //SendEmail(
            }
        }

        Response.Redirect("~/Server/device/default.aspx");
    }
    protected void ddlOS_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = ddlOS.SelectedIndex;
        if (index >= 0)
        {
            OS = OSs[index];
        }
    }
    protected void ddlCpu_SelectedIndexChanged(object sender, EventArgs e)
    {
        BillingCustomPlan(CPUs[ddlCpu.SelectedIndex].Price, Memorys[ddlMemory.SelectedIndex].Price, DiskPlaces[ddlDiskPlace.SelectedIndex].Price);
    }
    protected void ddlMemory_SelectedIndexChanged(object sender, EventArgs e)
    {
        BillingCustomPlan(CPUs[ddlCpu.SelectedIndex].Price, Memorys[ddlMemory.SelectedIndex].Price, DiskPlaces[ddlDiskPlace.SelectedIndex].Price);
    }
    protected void ddlDiskPlace_SelectedIndexChanged(object sender, EventArgs e)
    {
        BillingCustomPlan(CPUs[ddlCpu.SelectedIndex].Price, Memorys[ddlMemory.SelectedIndex].Price, DiskPlaces[ddlDiskPlace.SelectedIndex].Price);
    }
}